package com.sec.etech.employeesync.handler;

import com.sec.etech.employeesync.entity.OrgEmployeeZMajor;
import com.sec.etech.employeesync.mapper.OrgEmployeeZMajorMapper;
import com.sec.etech.employeesync.util.ConfigHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class MajorHandler implements ConfigHandler {

    @Resource
    private OrgEmployeeZMajorMapper majorMapper;

    @Override
    public Long createConfig(String... params) {
        String name = params[0];
        OrgEmployeeZMajor major = new OrgEmployeeZMajor();
        major.setName(name);
        major.setActive(1);
        majorMapper.insert(major);
        log.info("新增专业配置:name={}", name);

        return major.getMajorId();
    }

    @Override
    public List<ConfigEntry> getAllConfigs() {
        return majorMapper.selectList(null).parallelStream()
                .map(item -> {
                    ConfigEntry entry = new ConfigEntry();
                    entry.setId(item.getMajorId());
                    entry.setParams(new String[]{item.getName()});
                    return entry;
                })
                .collect(Collectors.toList());
    }
}
